//
//  AccountSavingView.m
//  SZBank
//
//  Created by rimi on 16/7/18.
//  Copyright © 2016年 LuoYao. All rights reserved.
//

#import "AccountSavingView.h"
#import "NSString+SizeOfString.h"
#define MYCOLOR [UIColor colorWithRed:0.227 green:0.349 blue:0.078 alpha:1.000]
@interface AccountSavingView()<ButtonAndTableViewDelegate>
{
    CGFloat frameWidth;/**  自身宽度 */
    CGFloat frameHeight;/**  自身高度 */
    NSMutableDictionary *sourceDic;
}
@property (nonatomic,strong) UIImageView *bgImageView;/**  背景图 */

@property (nonatomic,strong) UIScrollView *scrollView;/**  滚动视图 */
@end
@implementation AccountSavingView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        frameWidth = self.frame.size.width;
        frameHeight = self.frame.size.height;
        [self addSubview:self.bgImageView];
        
        sourceDic = [NSMutableDictionary dictionary];
        NSMutableArray *typeArray = [NSMutableArray array];/**  存款名称 */
        NSMutableArray *introduceArray = [NSMutableArray array];/**  产品简介 */
        NSMutableArray *featureArray = [NSMutableArray array];/**  产品特点 */
        NSMutableArray *incomeArray = [NSMutableArray array];/**  产品收益 */
        NSMutableArray *guideArray = [NSMutableArray array];/**  办理指南 */
        NSMutableArray *depositArray = [NSMutableArray array];/**  人民币存款 */
        /**  储蓄存款数据请求 */
        [NetWorkingDataManager getProtocolTemplateUpdateWithProtocolid:@"402886c2401fad1a0140282e801e0042" success:^(id sender) {
            
            NSInteger index = 0;
            for (NSDictionary *dic1 in sender) {
                NSDictionary *dic2 = [dic1 objectForKey:@"protocolData"];
                if (index > 4) {
                    [typeArray addObject:[dic2 objectForKey:@"存款名称"][0]];
                    [introduceArray addObject:[dic2 objectForKey:@"产品简介"][0]];
                    [featureArray addObject:[dic2 objectForKey:@"产品特点"][0]];
                    [incomeArray addObject:[dic2 objectForKey:@"产品收益"][0]];
                    [guideArray addObject:[dic2 objectForKey:@"办理指南"][0]];
                    [depositArray addObject:[dic2 objectForKey:@"人民币存款"][0]];
                }
                index ++;
            }
            [sourceDic setObject:typeArray[0] forKey:@"title"];
            [sourceDic setObject:typeArray forKey:@"list"];
            [sourceDic setObject:introduceArray forKey:@"产品简介"];
            [sourceDic setObject:featureArray forKey:@"产品特点"];
            [sourceDic setObject:incomeArray forKey:@"产品收益"];
            [sourceDic setObject:guideArray forKey:@"办理指南"];
            [sourceDic setObject:depositArray forKey:@"人民币存款"];
            
            [self initUserInterface];
        } failure:nil];

    }
    return self;
}

- (void)btnTableView:(ButtonAndTableView *)btnTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath selectTitle:(NSString *)selectTitle{
    [self.scrollView removeFromSuperview];
    [self addSubview:[self getScrollView:indexPath.row]];
}

- (void)initUserInterface{
    ButtonAndTableView *btView = [[ButtonAndTableView alloc] initWithFrame:CGRectMake(frameWidth * 0.02, frameHeight * 0.01, frameWidth * 0.96, frameHeight * 0.07) dataSource:sourceDic backImageName:@"储蓄存款_01" selectImageName:@"储蓄存款_01"];
    btView.delegate = self;
    [self addSubview:btView];
    [self addSubview:[self getScrollView:0]];
}

- (UIScrollView *)getScrollView:(NSInteger)index{
    /**  存款名称 */
    UILabel *nameTitle = [[UILabel alloc] initWithFrame:CGRectMake(0, frameHeight * 0.08, frameWidth * 0.1, frameHeight * 0.07)];
    [self getLabel:nameTitle textFont:[UIFont systemFontOfSize:28] textColor:MYCOLOR contentText:@"存款名称"];
    NSString *string1 = [sourceDic objectForKey:@"list"][index];
    CGFloat height1 = [NSString textString:string1 fontSize:24 maxWidth:frameWidth * 0.93].height;
    UILabel *name = [[UILabel alloc] initWithFrame:CGRectMake(frameWidth * 0.03, CGRectGetMaxY(nameTitle.frame), frameWidth * 0.93, height1)];
    [self getLabel:name textFont:[UIFont systemFontOfSize:24] textColor:[UIColor blackColor] contentText:string1];
    /**  产品简介 */
    UILabel *introduceTitle = [[UILabel alloc] initWithFrame:CGRectMake(0,  CGRectGetMaxY(name.frame), frameWidth * 0.1, frameHeight * 0.07)];
    [self getLabel:introduceTitle textFont:[UIFont systemFontOfSize:28] textColor:MYCOLOR contentText:@"产品简介"];
    NSString *string2 = [sourceDic objectForKey:@"产品简介"][index];
    CGFloat height2 = [NSString textString:string2 fontSize:24 maxWidth:frameWidth * 0.93].height;
    UILabel *introduce = [[UILabel alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(introduceTitle.frame), frameWidth * 0.93, height2)];
    [self getLabel:introduce textFont:[UIFont systemFontOfSize:24] textColor:[UIColor blackColor] contentText:string2];
    /**  产品特点 */
    UILabel *featureTitle = [[UILabel alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(introduce.frame), frameWidth * 0.1, frameHeight * 0.07)];
    [self getLabel:featureTitle textFont:[UIFont systemFontOfSize:28] textColor:MYCOLOR contentText:@"产品特点"];
    NSString *string3 = [sourceDic objectForKey:@"产品特点"][index];
    CGFloat height3 = [NSString textString:string3 fontSize:24 maxWidth:frameWidth * 0.93].height;
    UILabel *feature = [[UILabel alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(featureTitle.frame), frameWidth * 0.93, height3)];
    [self getLabel:feature textFont:[UIFont systemFontOfSize:24] textColor:[UIColor blackColor] contentText:string3];
    /**  产品收益 */
    UILabel *incomeTitle = [[UILabel alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(feature.frame), frameWidth * 0.1, frameHeight * 0.07)];
    [self getLabel:incomeTitle textFont:[UIFont systemFontOfSize:28] textColor:MYCOLOR contentText:@"产品收益"];
    NSString *string4 = [sourceDic objectForKey:@"产品收益"][index];
    CGFloat height4 = [NSString textString:string4 fontSize:24 maxWidth:frameWidth * 0.93].height;
    UILabel *income = [[UILabel alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(incomeTitle.frame), frameWidth * 0.93, height4)];
    [self getLabel:income textFont:[UIFont systemFontOfSize:24] textColor:[UIColor blackColor] contentText:string4];
    /**  办理指南 */
    UILabel *guideTitle = [[UILabel alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(income.frame), frameWidth * 0.1, frameHeight * 0.07)];
    [self getLabel:guideTitle textFont:[UIFont systemFontOfSize:28] textColor:MYCOLOR contentText:@"办理指南"];
    NSString *string5 = [sourceDic objectForKey:@"办理指南"][index];
    CGFloat height5 = [NSString textString:string5 fontSize:24 maxWidth:frameWidth * 0.93].height;
    UILabel *guide = [[UILabel alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(guideTitle.frame), frameWidth * 0.93, height5)];
    [self getLabel:guide textFont:[UIFont systemFontOfSize:24] textColor:[UIColor blackColor] contentText:string5];
    /**  人民币存款 */
    UILabel *depositTitle = [[UILabel alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(guide.frame), frameWidth * 0.15, frameHeight * 0.07)];
    [self getLabel:depositTitle textFont:[UIFont systemFontOfSize:28] textColor:MYCOLOR contentText:@"人民币存款"];
    NSString *string6 = [sourceDic objectForKey:@"人民币存款"][index];
    CGFloat height6 = [NSString textString:string6 fontSize:24 maxWidth:frameWidth * 0.93].height;
    UILabel *deposit = [[UILabel alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(depositTitle.frame), frameWidth * 0.2, height6)];
    [self getLabel:deposit textFont:[UIFont systemFontOfSize:24] textColor:[UIColor blackColor] contentText:string6];
    /**  滚动视图 */
    self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(frameWidth * 0.03, frameHeight * 0.1, frameWidth * 0.93, frameHeight * 0.88)];
    CGFloat totalHeight = height1 + height2 + height3 + height4 + height5 + height6 + 400;
    self.scrollView.contentSize = CGSizeMake(frameWidth * 0.03, totalHeight);
    self.scrollView.backgroundColor = [UIColor clearColor];
    
    [self.scrollView addSubview:nameTitle];
    [self.scrollView addSubview:name];
    [self.scrollView addSubview:introduceTitle];
    [self.scrollView addSubview:introduce];
    [self.scrollView addSubview:featureTitle];
    [self.scrollView addSubview:feature];
    [self.scrollView addSubview:incomeTitle];
    [self.scrollView addSubview:income];
    [self.scrollView addSubview:guideTitle];
    [self.scrollView addSubview:guide];
    [self.scrollView addSubview:depositTitle];
    [self.scrollView addSubview:deposit];
    
    return self.scrollView;
}

- (void)getLabel:(UILabel *)label textFont:(UIFont *)textFont textColor:(UIColor *)textColor contentText:(NSString *)contentText{
    label.font = textFont;
    label.textColor = textColor;
    label.text = contentText;
    label.numberOfLines = 0;
}

- (UIImageView *)bgImageView{
    if (!_bgImageView) {
        _bgImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, frameWidth, frameHeight)];
        _bgImageView.image = [LocalDataManager getImageWithSourceOfPath:@"储蓄存款背景"];
    }
    return _bgImageView;
}


@end
